package com.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.mapper.PlotMapper;
import com.pojo.Plot;
import com.service.FloormanagerService;
import com.service.PlotService;
import com.vo.TreeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class PlotServiceimpl implements PlotService {
    @Resource
    private PlotMapper plotMapper;

    @Resource
    private FloormanagerService floormanagerService;
    @Override
    public List<Plot> selectallPlot(Plot plot) {
        List<Plot> plots = plotMapper.selectallPlot( plot);
        return plots;
    }

    @Override
    public List<Plot> selectPlot() {
        List<Plot> plots = plotMapper.selectPlot();
        return plots;
    }

    @Override
    public int insert(Plot record) {
        //设置Code,全拼
        String pinyin= PinyinUtil.getPinyin(record.getPlotname(),"");
        record.setPlotCode(pinyin);
        plotMapper.insert(record);
        return 1;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        plotMapper.deleteByPrimaryKey(id);
        return 1;
    }

    @Override
    public int updateByPrimaryKey(Plot record) {
        //设置Code,全拼
        String pinyin= PinyinUtil.getPinyin(record.getPlotname(),"");
        record.setPlotCode(pinyin);
        plotMapper.updateByPrimaryKey(record);
        return 1;
    }

    @Override
    public Plot selectByPrimaryKey(Integer id) {
        Plot plot = plotMapper.selectByPrimaryKey(id);
        return plot;
    }

    @Override
    public List<TreeVo> queryTreeData() {
        List<Plot> plots = plotMapper.selectPlot();
        List<TreeVo> list=new ArrayList<>();
        for (Plot plot : plots) {
            TreeVo treeVo=transferTo(plot);
            list.add(treeVo);
        }
        return list;
    }
    public TreeVo transferTo(Plot plot){
        TreeVo treeVo=new TreeVo();
        treeVo.setId(plot.getId());
        treeVo.setTitle(plot.getPlotname());
        List<TreeVo> treeVoList = floormanagerService.queryTreefloorData(plot.getId());
        treeVo.setChildren(treeVoList);
        return treeVo;
    }
}
